package cn.com.blackboard.DataModule4B2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.blackboard.DataModule4B2.customizeConfigUtils.BatchImportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.CourseMembershipUtil;
import cn.com.blackboard.DataModule4B2.dao.TeacherCourseInfoMapper;
import cn.com.blackboard.DataModule4B2.model.TeacherCourseInfo;
import cn.com.blackboard.DataModule4B2.service.DepartmentService;
import cn.com.blackboard.DataModule4B2.service.TeacherCourseInfoService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;
import cn.com.blackboard.DataModule4B2.util.Condition;

@Service("TeacherCourseInfoService")
public class TeacherCourseInfoServiceImpl implements TeacherCourseInfoService{

	private static final Logger logger = LoggerFactory.getLogger(TeacherCourseInfoServiceImpl.class);
	@Autowired
	private TeacherCourseInfoMapper teacherCourseInfoMapper;

	@Autowired
	private DepartmentService departmentService;
	
	public int insert(TeacherCourseInfo TeacherCourseInfo) {
		// TODO Auto-generated method stub
		return teacherCourseInfoMapper.insert(TeacherCourseInfo);
	}

	@Override
	public List<TeacherCourseInfo> getTeacherCourseInfo() {
		return teacherCourseInfoMapper.selectByCondition(Condition.SearchAll);
	}

	@Override
	public int updateStatus(String TeacherCourseInfoId) {
		// TODO Auto-generated method stub
		return teacherCourseInfoMapper.updateRecordImportedByPK1(TeacherCourseInfoId);
	}
	
	@Override
	public List<TeacherCourseInfo> getTeacherCourseInfo(String jsonCondition) {
		Condition condition = Condition.ParseCondition(jsonCondition);
		String conditionStr = Condition.RenderConditoin(condition);
		return teacherCourseInfoMapper.selectByCondition(conditionStr);
	}

	@Override
	public List<BatchResultModel> importTeacherCourseInfo(List<TeacherCourseInfo> TeacherCourseInfoLst) {
		logger.info("start to import TeacherCourseInfo");
		int count = 0;
		List<BatchResultModel> list = new ArrayList<BatchResultModel>();
		for(TeacherCourseInfo tci:TeacherCourseInfoLst){
			BatchResultModel batchResultModel = new BatchResultModel();
			batchResultModel.setModelClsName(TeacherCourseInfo.class.getName());
			batchResultModel.setModelClsAttr("courseId-userId");
			batchResultModel.setValue(tci.getCourseId()+"-"+tci.getUserId());
			Status status = CourseMembershipUtil.importTeacherCourseMembership(tci);
			batchResultModel.setStatus(status);
			count += status.isSuccess()?1:0;
			if(status.isSuccess())
				updateStatus(tci.getUserId());
			list.add(batchResultModel);
		}
		logger.info("teacherEnrollment need to be imported is "+TeacherCourseInfoLst.size()+" the num imported indeed is "+count);
    	return list;
	}

	@Override
	public List<BatchResultModel> batchImportTeacherCourseInfo() {
		String conditionStr = BatchImportUtil.getBatchFilterCondition(TeacherCourseInfo.class.getName());
		List<TeacherCourseInfo> teacherCourseInfoList = teacherCourseInfoMapper.selectByCondition(conditionStr);
		List<BatchResultModel> list = importTeacherCourseInfo(teacherCourseInfoList);
		return list;
	}
}
